'use strict';

const Controller = require('egg').Controller;

class ExpertController extends Controller {

  async getExpertCollectList() {
    const { ctx } = this;
    const parentId = ctx.getQuery('parentId');
    const [ list ] = await ctx.service.expert.getExpertCollectList(
      { parentId },
    );
    ctx.body = {
      success: true,
      list,
    }
  }

  async getExpertParentCollectList() {
    const { ctx } = this;
    const [ list ] = await ctx.service.expert.getExpertParentCollectList();
    ctx.body = {
      success: true,
      list,
    }
  }

  async createExpertCollect() {
    const { ctx } = this;
    const body = ctx.request.body;
    const [ id ] = ctx.service.expert.newExpertCollect({
      name: body.name,
      parentId: body.parentId,
      isParent: body.isParent,
      sort: body.sort,
    });

    ctx.body = {
      success: true,
      id
    }
  }
  async getExpertFollowList() {
    const { ctx } = this;
    const listQuery = {
      user_id: ctx.getUserId(),
      follow: true,
    };
    const [ option ] = ctx.getPageListOption();
    option.sort = '-update_at';

    const [ followList, totalCount ] = await ctx.service.expertFollow.getFollowListByQuery(
      listQuery,
      option,
    );

    ctx.apiPageList(followList, totalCount);
  }

  async getMoreExpertEvaluateList() {
    const { ctx } = this;
    const [ pageNum, pageSize ] = ctx.getPageInfo();
    const [ evaluateList, totalCount ] = await ctx.service.expertEvaluation.getEvaluationListByQuery({
      pageNum, pageSize,
    });
    ctx.apiPageList(evaluateList, totalCount);
  }

  async getLightExpertDetail() {
    const { ctx } = this;
    const query = ctx.request.query;
    const [ expert ] = await ctx.service.expert.getLightExpertDetail({
      expertId: query.expertId,
    });
    ctx.body = {
      success: true,
      expertDetail: expert,
    };
  }

  async getExpertFollowDetail() {
    const { ctx } = this;

    const query = ctx.request.query;

    const [ expertFollow ] = await ctx.service.expert.getExpertFollowDetail({
      expertId: query.expertId,
      userId: ctx.getUserId(),
    });

    ctx.body = {
      success: true,
      expertFollow,
    };
  }

  async getExpertDetail() {
    const { ctx } = this;
    const expertId = ctx.getQuery('expertId');

    const [ expert, evaluateList, totalCount, expertFollow ] = await ctx.service.expert.getFullExpertDetail({
      expertId,
    });

    ctx.body = {
      success: true,
      expertDetail: expert,
      evaluateList: evaluateList,
      totalCount: totalCount,
      expertFollow,
    };
  }

  async getExpertList() {
    const { ctx } = this;
    
    const listQuery = {};
    const [ option ] = ctx.getPageListOption();

    const expertCollect = ctx.getQuery('expertCollect');
    const listSort = ctx.getQuery('listSort');
    const serviceSort = ctx.getQuery('serviceSort');
    const priceSection = ctx.getQuery('priceSection');
    const physicianSection= ctx.getQuery('physicianSection');

    if (expertCollect) {
      listQuery['expert_collect.collect'] = expertCollect;
    }

    if (listSort) {
      switch(listSort) {
        case 'default':
          option.sort = '-create_at';
          break;
        case 'help':
          option.sort = '+help_total_count';
          break;
        case 'evaluate':
          option.sort = '+evaluate_total_count';
          break;
      }
    }

    if (serviceSort) {
      option.sort = { 'services.amount': -1 }
    }
    

    if (priceSection) {
      const [ min, max ] = priceSection.split(',');
      listQuery['services.amount'] = { $gte: Number(min) * 100, $lt: Number(max) * 100 };
    }

    if (physicianSection) {
      listQuery['physician_key'] = physicianSection;
    }

    const expertList = await ctx.service.expert.getExpertList(listQuery, option);
    const totalCount = await ctx.service.expert.getCountByQuery(listQuery);

    ctx.apiPageList(expertList, totalCount);
  }

  async postFollow() {
    const { ctx } = this;
    const body = ctx.request.body;
    const user = ctx.request.user;

    const {
      expertId,
    } = body;

    try {
      await ctx.service.expertFollow.postExertFollow({
        expertId,
        userId: user.userId,
      });
  
      ctx.body = { success: true };
      return;
    } catch(e) {
      ctx.body = { success: false, error: e };
    }
  }

  async cancelFollow() {
    const { ctx } = this;
    const body = ctx.request.body;
    const user = ctx.request.user;
    const {
      expertId,
    } = body;

    try {
      const [ result ] = await ctx.service.expertFollow.cancelExpertFollow({
        expertId,
        userId: user.userId,
      });
      if (result) {
        ctx.body = {
          success: true,
        }
        return;
      }
      ctx.body = {
        success: false,
      }
      return;
    } catch(e) {
      ctx.body = {
        success: false,
        error: e,
      }
    }
  }


  async create() {
    const { ctx } = this;
    const body = ctx.request.body;
    
    const expert = await ctx.service.expert.newAndSave({
      name: body.name,
      hospital: body.hospital,
      hospital_level: body.hospital_level,
      physician: body.physician,
      categray: body.categray,
      metier: body.metier,
      avatar_image: body.avatar_image,
      shanchang: body.shanchang,
      services: body.services,
      goodat: body.goodat,
      score: body.score,
      help_total_count: body.help_total_count,
      follow_total_count: body.follow_total_count,
    });

    ctx.body = {
      success: true,
      expert_id: expert.id,
    };
  }

  async createEvaluation() {
    const { ctx } = this;
    const body = ctx.request.body;
    const user = ctx.request.user;

    const countEvaluate = await ctx.service.expertEvaluation.countByQuery({
      orderId: body.orderId,
    })

    if (countEvaluate > 0) {
      ctx.body = {
        success: true,
        hasAreadyEvaluate: true,
      };
      return;
    }

    const evaluation = await ctx.service.expertEvaluation.newAndSave({
      order_id: body.orderId,
      expert_id: body.expertId,
      user_id: user.userId,
      score: body.score,
      content: body.content,
      tags: body.tags,
    });

    ctx.body = {
      success: true,
      hasAreadyEvaluate: false,
      expert_id: evaluation.id,
    };
  }
}

module.exports = ExpertController;
